package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.DTO.IotMsgNotifyData;
import com.zzyl.nursing.DTO.IotMsgService;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.mapper.DeviceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IDeviceDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 设备数据上报Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-18
 */
@Service
@Slf4j
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements IDeviceDataService
{
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询设备数据上报
     * 
     * @param id 设备数据上报主键
     * @return 设备数据上报
     */
    @Override
    public DeviceData selectDeviceDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询设备数据上报列表
     * 
     * @param deviceData 设备数据上报
     * @return 设备数据上报
     */
    @Override
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData)
    {
        return deviceDataMapper.selectDeviceDataList(deviceData);
    }

    /**
     * 新增设备数据上报
     * 
     * @param deviceData 设备数据上报
     * @return 结果
     */
    @Override
    public int insertDeviceData(DeviceData deviceData)
    {
        return save(deviceData) ? 1 : 0;
    }

    /**
     * 修改设备数据上报
     * 
     * @param deviceData 设备数据上报
     * @return 结果
     */
    @Override
    public int updateDeviceData(DeviceData deviceData)
    {
        return updateById(deviceData) ? 1 : 0;
    }

    /**
     * 批量删除设备数据上报
     * 
     * @param ids 需要删除的设备数据上报主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除设备数据上报信息
     * 
     * @param id 设备数据上报主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }


    /***
     *批量新增设备数据
     * @param bean
     */
    @Override
    public void batchSaveDeviceData(IotMsgNotifyData bean) {
     //1.根据设备ID查血设备信息
        String deviceId = bean.getHeader().getDeviceId();
        Device device = deviceMapper.selectOne(new LambdaQueryWrapper<Device>().eq(Device::getIotId, deviceId));
        //2.判断设备是否存在
        if (ObjectUtil.isEmpty(device) ) {
            log.error("设备不存在,iotId = {}",deviceId);
        }
        //3.获取设备上报的数据，保存到数据库
        List<IotMsgService> services = bean.getBody().getServices();
        for (IotMsgService service : services) {
            Map<String, Object> properties = service.getProperties();
            if(ObjectUtil.isEmpty(properties)){
                continue;
            }
            LocalDateTime parse = LocalDateTimeUtil.parse(service.getEventTime(), "yyyyMMdd'T'HHmmss'Z'");
            LocalDateTime localDateTime = parse.plusHours(8);

            List<DeviceData> Lists = new ArrayList<>(properties.size());
            properties.forEach((k, v) -> {
                DeviceData build = DeviceData.builder()
                        .iotId(deviceId)
                        .functionId(k)
                        .dataValue(v.toString())
                        .alarmTime(localDateTime)
                        .accessLocation(device.getRemark())
                        .build();
                BeanUtil.copyProperties(device, build, "id","createBy","updateBy","createTime","updateTime");
                Lists.add(build);
            });
            this.saveBatch(Lists);

        }
    }
}
